﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class ArticleTypeList : System.Web.UI.Page
    {
        protected ArticleType _articleType = new ArticleType();
        protected DataTable _dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["memberID"].ToString() != "" && Session["memberID"].ToString() == "1")
            {
                try
                {
                    if (Request.Form["ArticleIDDelete"] != null)
                    {
                        string _articleTypeID = Request.Form["ArticleIDDelete"].ToString();
                        _articleType.ArticleTypeDelete(_articleTypeID);
                    }

                    if (!IsPostBack)
                    {

                        ddlArticleTypeSuperior.Items.Insert(0, new ListItem("-- Chọn --", "0"));
                        ddlArticleTypeSuperior.Items.Insert(1, new ListItem("Trang nhất", "1"));
                        ddlArticleTypeSuperior.Items.Insert(2, new ListItem("Sức khỏe", "2"));
                        ddlArticleTypeSuperior.Items.Insert(3, new ListItem("Dinh dưỡng", "3"));
                        ddlArticleTypeSuperior.Items.Insert(4, new ListItem("Giảng dạy", "4"));
                        ddlArticleTypeSuperior.Items.Insert(5, new ListItem("Tin tức tháng", "5"));

                        // load drop down list article type
                        DataTable dtArticleType = _articleType.GetAllArticleType3();
                        ddlArticleTypeSub.DataSource = dtArticleType;
                        ddlArticleTypeSub.DataTextField = "ArticleTypeName";
                        ddlArticleTypeSub.DataValueField = "ArticleTypeID";
                        ddlArticleTypeSub.DataBind();
                        ddlArticleTypeSub.Items.Insert(0, new ListItem("-- Chọn --", "0"));

                        ddlArticleTypeSuperior2.Items.Insert(0, new ListItem("-- Chọn --", "0"));
                        ddlArticleTypeSuperior2.Items.Insert(1, new ListItem("Trang nhất", "1"));
                        ddlArticleTypeSuperior2.Items.Insert(2, new ListItem("Sức khỏe", "2"));
                        ddlArticleTypeSuperior2.Items.Insert(3, new ListItem("Dinh dưỡng", "3"));
                        ddlArticleTypeSuperior2.Items.Insert(4, new ListItem("Giảng dạy", "4"));
                        ddlArticleTypeSuperior2.Items.Insert(5, new ListItem("Tin tức tháng", "5"));

                    }

                    _dt = _articleType.GetAllArticleType2();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else { Response.Redirect(ConstantClass._errorAdminPage); }
        }

        protected void btArticleTypeCreate_Click(object sender, EventArgs e)
        {
            if (txtArticleTypeName.Text.Trim() != "" && ddlArticleTypeSuperior.SelectedValue != "0")
            {
                try
                {
                    _articleType.ArticleTypeCreate(txtArticleTypeName.Text.Trim(), ddlArticleTypeSuperior.SelectedValue);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
                Response.Redirect("ArticleTypeList.aspx");
            }
        }

        protected void txtArticleTypeUpdate_Click(object sender, EventArgs e)
        {
            if (ddlArticleTypeSub.SelectedValue != "0" && ddlArticleTypeSuperior2.SelectedValue != "0")
            {
                try
                {
                    _articleType.ArticleTypeUpdate(ddlArticleTypeSub.SelectedValue, ddlArticleTypeSuperior2.SelectedValue, txtArticleTypeNameUpdate.Text.Trim());

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
                Response.Redirect("ArticleTypeList.aspx");
            }
        }
    }
}